package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import com.fwtai.web.SmsDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 短信发送业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-11-12 16:16:08
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class SmsService{

    @Resource
    private SmsDao smsDao;

    public String listData(PageFormData formData){
        final String date_start = formData.getString("date_start");
        if(date_start != null){
            final String msg = "起始日期时间格式有误";
            if(date_start.length() != 19){
                return ToolClient.createJsonFail(msg);
            }
            final boolean b = ToolString.checkDateTimes(date_start);
            if(!b){
                return ToolClient.createJsonFail(msg);
            }
        }
        final String date_end = formData.getString("date_end");
        if(date_end != null){
            final String msg = "截至日期时间格式有误";
            if(date_end.length() != 19){
                return ToolClient.createJsonFail(msg);
            }
            final boolean b = ToolString.checkDateTimes(date_end);
            if(!b){
                return ToolClient.createJsonFail(msg);
            }
        }
        try {
            formData = ToolClient.dataTableMysql(formData);
            if(formData == null)return ToolClient.jsonValidateField();
            final HashMap<String,Object> map = smsDao.listData(formData);
            return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
        } catch (final Exception e){
            return ToolClient.dataTableException();
        }
    }

    public String listInfo(PageFormData formData){
        final String p_kid = "kid";
        final String validate = ToolClient.validateField(formData,p_kid);
        if(validate != null)return validate;
        try {
            formData = ToolClient.dataTableMysql(formData);
            if(formData == null)return ToolClient.jsonValidateField();
            final HashMap<String,Object> map = smsDao.listInfo(formData);
            return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
        } catch (final Exception e){
            return ToolClient.dataTableException();
        }
    }
}